package com.iwop.moto.controller.web;

import com.iwop.moto.comm.Comm;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.IFGoodsService;
import com.iwop.moto.utils.file.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 功能: 摩配信息管理
 * 说明: 新增/修改/查询
 * 数据表: F_GOODS_NEW (新) / F_GOODS (旧)
 * @author  Created by liuwb on 2016-07-15.
 */
@Controller
public class FGoodsController extends BaseController {

    public static final String ImageFilePath="image/dev-pic/";

    @Autowired
    private IFGoodsService fgoodsService;

    //String basePath = "D:/";
    /**
     * 功能说明:配件信息 - 列表
     * liuwb 2016-07-15 21:03
     */
    @RequestMapping(value="/fgoods/list")
    public ModelAndView fgoodsList() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        if(SRModel.get("currentPage")==null || Integer.valueOf(SRModel.get("currentPage").toString())<=1){
            SRModel.put("currentResult",0);
            SRModel.put("currentPage",1);
        }else{
            SRModel.put("currentResult", (Integer.valueOf(SRModel.get("currentPage").toString()) - 1) * Integer.valueOf(SRModel.get("showCount").toString()));
        }
        if(SRModel.get("showCount")==null){
            SRModel.put("showCount",15);
        } else {
            SRModel.put("showCount",Integer.valueOf(SRModel.get("showCount").toString()));
        }
        List<SuperRecordModel>  srm = fgoodsService.findFGoodsListSRModel(SRModel);
        SuperRecordModel srmTotal = fgoodsService.findFGoodsTotalPage(SRModel);
        SRModel.put("totalPage",srmTotal.get("totalPage"));
        SRModel.put("totalCount",srmTotal.get("totalCount"));
        mv.setViewName("fgoodslist");
        mv.addObject("srm", srm);
        mv.addObject("SRModel", SRModel);
        return mv;
    }

    /**
     * 功能说明:摩配信息 - 编辑信息
     * liuwb 2016-07-15
     */
    @RequestMapping(value="/fgoods/edit")
    public ModelAndView fgoodsEdit() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        SRModel = fgoodsService.selectByPrimaryKey(Long.valueOf(SRModel.get("id").toString()));
        String  content = SRModel.get("content").toString();
        SRModel.put("content",content.substring(content.indexOf("<"), content.lastIndexOf(">")+1));
        mv.setViewName("fgoodsedit");
        mv.addObject("SRModel", SRModel);
        return mv;
    }

    /**
     * 功能说明:摩配信息 - 编辑保存
     * liuwb 2016-07-15
     */
    @RequestMapping(value="/fgoods/editsave")
    public ModelAndView fgoodsEditSave(
            @RequestParam(value="id",required=false) Long id,
            @RequestParam(value="goodstype",required=false) String goodstype,
            @RequestParam(value="goodstwotype",required=false) String goodstwotype,
            @RequestParam(value="goodsname",required=false) String goodsname ,
            @RequestParam(value="goodsbrand",required=false) String goodsbrand ,
            @RequestParam(value="goodsmodspCodesTempel",required=false) String goodsmodel ,
            @RequestParam(value="status",required=false) String status,
            @RequestParam(value="province",required=false) String province,
            @RequestParam(value="city",required=false) String city,
            @RequestParam(value="nick",required=false) String nick,
            @RequestParam(value="url",required=false) String url,
            @RequestParam(value="price",required=false) String price,
            @RequestParam(value="orgprice",required=false) String orgprice,
            @RequestParam(value="currency",required=false) String currency,
            @RequestParam(value="score",required=false) String score,
            @RequestParam(value="atrrs",required=false) String atrrs,
            @RequestParam(value="content",required=false) String content,
            @RequestParam(value="img_url1",required=false) String img_url1,
            @RequestParam(value="img_url2",required=false) String img_url2,
            @RequestParam(value="img_url3",required=false) String img_url3,
            @RequestParam(value="img_url4",required=false) String img_url4,
            @RequestParam(value="img_url5",required=false) String img_url5,
            @RequestParam(value="img_1",required=false) MultipartFile img_1,
            @RequestParam(value="img_2",required=false) MultipartFile img_2,
            @RequestParam(value="img_3",required=false) MultipartFile img_3,
            @RequestParam(value="img_4",required=false) MultipartFile img_4,
            @RequestParam(value="img_5",required=false) MultipartFile img_5
    ) {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = new SuperRecordModel();
        SRModel.put("id",id);
        SRModel.put("goodstype",goodstype);
        SRModel.put("goodstwotype",goodstwotype);
        SRModel.put("goodsname",goodsname);
        SRModel.put("goodsbrand",goodsbrand);
        SRModel.put("goodsmodel",goodsmodel);
        SRModel.put("status",status);
        SRModel.put("province",province);
        SRModel.put("city",city);
        SRModel.put("nick",nick);
        SRModel.put("url",url);
        SRModel.put("price",price);
        SRModel.put("orgprice",orgprice);
        SRModel.put("currency",currency);
        SRModel.put("score",score);
        SRModel.put("atrrs",atrrs);
        SRModel.put("content",content);
        if(img_url1!=null && !"".equals(img_url1.trim())){
            SRModel.put("img_url1",img_url1);
        }
        if(img_url2!=null && !"".equals(img_url2.trim())){
            SRModel.put("img_url2",img_url2);
        }
        if(img_url3!=null && !"".equals(img_url3.trim())){
            SRModel.put("img_url3",img_url3);
        }
        if(img_url4!=null && !"".equals(img_url4.trim())){
            SRModel.put("img_url4",img_url4);
        }
        if(img_url5!=null && !"".equals(img_url5.trim())){
            SRModel.put("img_url5",img_url5);
        }
        try {
            String basePath = this.getRequest().getSession().getServletContext().getRealPath("/");
            if(img_1 != null && !"".equals(img_1.getOriginalFilename())){
                delFiel(basePath + SRModel.getString("img_url1"));
                SRModel.put("img_url1", FileUtil.uploadImg(img_1, Comm.ROOT_PATH));
            }
            if(img_2 != null && !"".equals(img_2.getOriginalFilename())){
                delFiel(basePath + SRModel.getString("img_url2"));
                SRModel.put("img_url2",FileUtil.uploadImg(img_2,Comm.ROOT_PATH));
            }
            if(img_3 != null && !"".equals(img_3.getOriginalFilename())){
                delFiel(basePath + SRModel.getString("img_url3"));
                SRModel.put("img_url3", FileUtil.uploadImg(img_3, Comm.ROOT_PATH));
            }
            if(img_4 != null && !"".equals(img_4.getOriginalFilename())){
                delFiel(basePath + SRModel.getString("img_url4"));
                SRModel.put("img_url4",FileUtil.uploadImg(img_4, Comm.ROOT_PATH));
            }
            if(img_5 != null && !"".equals(img_5.getOriginalFilename())){
                delFiel(basePath + SRModel.getString("img_url5"));
                SRModel.put("img_url5",FileUtil.uploadImg(img_5,Comm.ROOT_PATH));
            }
        }  catch (Exception e) {
            e.printStackTrace();
        }
        fgoodsService.updateByPrimaryKeySelective(SRModel);
        mv.setViewName("fgoodsedit");
        mv.addObject("SRModel", SRModel);
        mv.addObject("upFlag",true);
        return mv;
    }

    /**
     * 功能:更新信息状态(http)
     * 参数:id 主键
     * 参数:status 状态 [-1:弃用][0:未确认][1:已确认][2:已完成]
     */
    @RequestMapping(value="/upfgoods/status")
      public ModelAndView upFgoodsStatus() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("id",Long.valueOf(SRModel.get("id").toString()));
        SRModel.put("addtime",new Date());
        fgoodsService.updateFgoodsStatus(SRModel);
        mv.setViewName("fgoodsedit");
        mv.addObject("SRModel", SRModel);
        mv.addObject("upFlag",true);
        return mv;
    }

    /**
     * 功能:更新信息状态(ajax)
     * 参数:id 主键
     * 参数:status 状态 [-1:弃用][0:未确认][1:已确认][2:已完成]
     */
    @RequestMapping(value="/ajax/upfgoods/status")
    @ResponseBody
    public ResultModel ajaxUpFgoodsStatus() {
        ResultModel resultModel=new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("id",Long.valueOf(SRModel.get("id").toString()));
        SRModel.put("addtime",new Date());
        fgoodsService.updateFgoodsStatus(SRModel);
        resultModel.setCode(0);
        resultModel.setMem("成功");
        return resultModel;
    }

    /**
     * 功能:批量更新信息状态(ajax)
     * 参数:ids 主键 (多条按逗号,分隔)
     * 参数:status 状态 [-1:弃用][0:未确认][1:已确认][2:已完成]
     */
    @RequestMapping(value="/ajax/upfgoods/all/status")
    @ResponseBody
    public ResultModel ajaxAllUpFgoodsStatus() {
        ResultModel resultModel=new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        String [] arr = SRModel.get("ids").toString().split(",");
        for(String str : arr){
            SRModel.put("id",Long.valueOf(str));
            SRModel.put("addtime",new Date());
            fgoodsService.updateFgoodsStatus(SRModel);
        }
        resultModel.setCode(0);
        resultModel.setMem("成功");
        return resultModel;
    }

    /**
     * 功能:删除 - 编辑信息中的图片
     * 说明:删除图片url以及对应的图片
     * 参数:id 主键
     */
    @RequestMapping(value="/ajax/del/img")
    @ResponseBody
    public ResultModel ajaxDelImg() {
        ResultModel resultModel=new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("id",Long.valueOf(SRModel.get("id").toString()));
        fgoodsService.delFgoodsImg(SRModel);
        resultModel.setCode(0);
        resultModel.setMem("成功");
        String basePath = this.getRequest().getSession().getServletContext().getRealPath("/");
        delFiel(basePath + SRModel.getString("urlPath"));
        return resultModel;
    }

    /**
     * 功能:删除图片
     * @param path 根据url路径
     */
    public void delFiel(String path){
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }
    }




}
